<?php
// 本类由系统自动生成，仅供测试用途
class ProductAction extends AdminAction{
	public function _initialize() {
        parent::_initialize();  //RBAC 验证接口初始化
    }

	private $root_name="";
	private $site_root='';
	private $file_noexist = 'file_noexist';
    private $dele_statice_success = 'dele_statice_success';
    private $dele_statice_fail = 'dele_statice_fail';
    private $delete_success = 'delete_success';
    private $delete_fail = 'delete_fail';
    private $statice_success = 'statice_success';
    private $statice_fail = 'staice_fail';
    private $pro_no_statice_name = 'pro_no_statice_name';
    private $art_no_statice_name = 'art_no_statice_name';
    private $howto_no_statice_name = 'howto_no_statice_name';
    private $plate_no_statice_name = 'plate_no_statice_name';
    public function __construct(){
    	$this->root_name = 'http://'.$_SERVER['HTTP_HOST'].'/index.php';
    	$this->site_root = 'http://'.$_SERVER['HTTP_HOST'];
    	parent::__construct();

    }

	 //添加一级分类
	public function big_category(){

		if($this->isPost()){
			$bigcategory=M('Bigcategory');
    		$_POST['add_time']=time();
    		if(!$_POST['big_category_name']){
    			return;
    		}
    		if($bigcategory->where("big_category_name='".$_POST['big_category_name']."'")->find()){
    			echo "<script>alert('分类名已存在，请填写其他分类名');history.back();</script>";
    			return;
    		}
			if(!$_FILES){
    			return;
    		}
    		$updata=$this->up();
    		if($updata){
				$_POST['big_img']=$updata[0]['savename'];
    		}else{
    			echo "<script>alert('上传失败');history.back();</script>";
    			return;
    		}
    		if($bigcategory->add($_POST)){
    			echo "<script>alert('添加成功');history.back();</script>";
    		}else{
    			echo "<script>alert('添加失败');history.back();</script>";
    		}

    	}else{
    		$this->display();
    	}
    }

	 //栏目管理
    public function plate_manage(){

    	$bigcategory=M('Bigcategory');
    	$cate=$bigcategory->select();
    	$product=M('Product');
    	$article=M('Article');
    	foreach($cate as $k => $v){
    		$cate[$k]['art_num']=$article->where('pid='.$v['id'])->count();
    		$cate[$k]['pro_num']=$product->where('pid='.$v['id'])->count();
    	}
    	var_dump(C('RBAC_ERROR_PAGE'));
    	$this->assign('cate',$cate);
    	$this->display();
    }

	//更改大类名称
     public function change_big_cate(){

     	if($this->isPost()){
     		$id=isset($_POST['cate_id'])?intval($_POST['cate_id']):0;

     		if(!$_POST['cate_id'] || !$_POST['big_category_name']){
     			return;
     		}
     		$bigcategory=M('Bigcategory');
//     		if($bigcategory->where("id!=".$id." and big_category_name='".$_POST['big_category_name']."'")->find()){
//    			echo "<script>alert('分类名已存在，请填写其他分类名');history.back();</script>";
//    			return;
//    		}

    		$updata=$this->up();
    		if($updata){
				$_POST['big_img']=$updata[0]['savename'];
    		}
     		if($bigcategory->where("id=$id")->save($_POST)){
     			echo "<script>alert('修改成功');location.href='".__URL__."/plate_manage';</script>";
     		}else{
     			echo "<script>alert('修改失败');history.back();</script>";
     		}
     	}else{
	     	$bigcategory=M('Bigcategory');
	     	$id=$_GET['id'];
	     	if(!$id){
	     		return;
	     	}
	     	$data=$bigcategory->find($id);
	     	$this->assign('cate',$data);
	     	$this->display();
     	}
     }

	//静态化howto页
 	public function howto_statice(){
		header("Content-Type:text/html; charset=utf-8");
    	$id=$_GET['id'];
    	if(!$id){
    		return;
    	}
    	$bigcategory=M('Bigcategory');
    	$data=$bigcategory->find($id);
    	if(!$data['statice_name']){
//    		echo "<script>alert('请先为大类添加howto页静态化名称');location.href='".__URL__."/change_big_cate/id/".$id."';</script>";
    		echo $this->howto_no_statice_name;
    		return;
    	}
		$p=file_get_contents($this->root_name.'/Index/howto/id/'.$id.'/type/select');
	   	if(file_put_contents($data['statice_name'].'.html', $p)){
	   		$bigcategory->where('id='.$id)->save(array('is_statice'=>'1'));
//	   		echo "<script>alert('静态化成功');location.href='".__URL__."/plate_manage';</script>";
			echo $this->statice_success;
			return;
	   	}else{
//    		echo "<script>alert('静态化失败');history.back();</script>";
    		echo $this->statice_fail;
    	}
    }



	//删除howto静态化页面
    Public function delete_howto_statice(){
    	header("Content-Type:text/html; charset=utf-8");
    	$id=$_GET['id'];
    	if(!$id){
    		return;
    	}
    	$bigcategory=M('Bigcategory');
    	$bdata=$bigcategory->find($id);

     	//删除相关静态化文件
     	if($bdata['statice_name'] && file_exists($bdata['statice_name'].'.html')){
     		unlink($bdata['statice_name'].'.html');
     	}else{
//     		echo "<script>alert('静态化文件不存在');history.back();</script>";
     		echo $this->file_noexist;
     		return;
     	}
     	$data['is_statice']=0;
    	if($bigcategory->where("id=$id")->save($data)){
//     			echo "<script>alert('删除成功');location.href='".__URL__."/plate_manage';</script>";
			echo $this->dele_statice_success;
			return;
     	}else{
//     		echo "<script>alert('删除失败');history.back();</script>";
			echo $this->dele_statice_fail;
     	}
    }



	//删除静态化栏目页----没有的
 	public function delete_plate_statice(){
 		header("Content-Type:text/html; charset=utf-8");
    	$id=$_GET['id'];
    	$pid=$_GET['pid'];
    	if(!$id || !$pid){
    		return;
    	}
    	$plate=M('Plate');
    	$pdata=$plate->find($id);
 		//删除相关静态化文件
     	if($pdata['statice_name'] && file_exists($pdata['statice_name'].'.html')){
     		unlink($pdata['statice_name'].'.html');
     	}else{
//     		echo "<script>alert('静态化文件不存在');history.back();</script>";
     		echo $this->file_noexist;
     		return;
     	}
     	$data['is_statice']=0;
    	if($plate->where("id=$id")->save($data)){
//     		echo "<script>alert('删除成功');location.href='".__URL__."/plate_manage';</script>";
     		echo $this->dele_statice_success;
     		return;
     	}else{
//     		echo "<script>alert('删除失败');history.back();</script>";
			echo $this->dele_statice_fail;
			return;
     	}
    }

	//删除产品静态化栏目页---没有的
 	public function delete_product_statice(){
 		header("Content-Type:text/html; charset=utf-8");
    	$id=$_GET['id'];
    	if(!$id){
    		return;
    	}
    	$product=M('Product');
    	$pdata=$product->find($id);
 		//删除相关静态化文件
     	if($pdata['statice_name'] && file_exists($pdata['statice_name'].'.html')){
     		unlink($pdata['statice_name'].'.html');
     	}else{
     		echo $this->file_noexist;
     		return;
     	}
     	$data['is_statice']=0;
    	if($product->where("id=$id")->save($data)){
     		echo $this->dele_statice_success;
     		return;
     	}else{
     		echo $this->dele_statice_fail;
     		return;
     	}

    }




     //删除静态化文件   传入一个数组
     function delete_statice_file($arr){
     	if(is_array($arr)){
     		foreach($arr as $k => $v){
     			if($v['statice_name'] && file_exists($v['statice_name'].'.html')){
     				unlink($v['statice_name'].'.html');
     			}
     		}
     	}
     }



	//删除大类
    public function delete_big_cate(){
    	header("Content-Type:text/html; charset=utf-8");

     	$id=$_GET['id'];
      	if (!$id){
     		return;
     	}

     	$bigcategory=M('Bigcategory');
     	$plate=M('Plate');
     	$article=M('Article');
     	$product=M('Product');
     	$bdata=$bigcategory->find($id);
     	$pdata=$plate->where('pid='.$id)->select();
     	$prodata=$product->where('gid='.$id)->select();
     	$adata=$article->where('gid='.$id)->select();

     	//删除大类同时删除相关静态化文件
     	if($bdata['statice_name'] && file_exists($bdata['statice_name'].'.html')){
     		unlink($bdata['statice_name'].'.html');
     	}
     	$this->delete_statice_file($pdata);
     	$this->delete_statice_file($prodata);
     	$this->delete_statice_file($adata);

     	$bret=$bigcategory->where("id=$id")->delete();
     	$pret=$plate->where("pid=$id")->delete();
     	$aret=$article->where("gid=$id")->delete();
     	$prret=$product->where("gid=$id")->delete();
     	if(!$bret){
//     		echo "<script>alert('删除大类失败');history.back();</script>";
     		echo $this->delete_fail;
     		return;
     	}else{
//     		echo "<script>alert('删除大类成功');location.href='".__URL__."/plate_manage';</script>";
			echo $this->delete_success;
     		return;
     	}
     }


	//产品管理
    public function product_manage(){

    	$product=M('Product');
    	$bigcategory=M('Bigcategory');
    	$plate=M('Plate');
    	$cate=$bigcategory->select();
    	$this->assign('cate',$cate);
    	$pid=$_GET['pid'];
    	if(isset($pid) && $pid != ''){
    		$pdata=$product->where('pid='.$pid)->select();
    		$this->assign('b_id',$pid);
    	}else{
    		$pdata=$product->select();
    	}
    	foreach ($pdata as $k => $v){
	    	$pdata[$k]['cate_data']=$bigcategory->where('id='.$v['pid'])->find();
//    		$pdata[$k]['plate_data']=$plate->where('id='.$v['pid'])->find();
    	}
    	$this->assign('product',$pdata);
    	$this->display();

    }

	 //添加产品
	public function product(){

		if($this->isPost()){

			$product=M('Product');
			$id=isset($_POST['plate'])?explode(',', $_POST['plate']):'';
			$_POST['gid']=$id[0];
			$_POST['relative_product']=isset($_POST['rp'])?trim($_POST['rp'],','):'';

			if (get_magic_quotes_gpc()) {

				$_POST['content'] = isset($_POST['product_content'])?stripslashes($_POST['product_content']):'';
				$_POST['sec_description'] = isset($_POST['sec_description'])?stripslashes($_POST['sec_description']):'';
			}else{

				$_POST['content'] = $_POST['product_content'];

			}
    		$_POST['add_time']=time();
    		$width=isset($_POST['width'])?trim($_POST['width']):0;
    		$height=isset($_POST['height'])?trim($_POST['height']):0;
			echo $_POST['product_name'];
    		if(!$_POST['pid'] || !$_POST['product_name']){
    			return;
    		}

			if($product->where("pid=".$_POST['pid']." and product_name='".$_POST['product_name']."'")->find()){
    			echo "<script>alert('产品名已存在，请填写其他产品名');history.back();</script>";
    			return;
    		}
    		if(!$_FILES){
    			return;
    		}
    		$updata=$this->up($width,$height);

    		if($updata){
    			foreach($updata as $v){
    				if($v['key'] == 't_img'){
    					$_POST['product_img']=$v['savename'];
    				}elseif($v['key'] == 'sec_img'){
    					$_POST['sec_img']=$v['savename'];
    				}elseif($v['key'] == 'three_img'){
    					$_POST['three_img']=$v['savename'];
    				}elseif($v['key'] == 'home_img'){
    					$_POST['home_img']=$v['savename'];
    				}
    			}
    		}else{
    			echo "<script>alert('上传失败');history.back();</script>";
    			return;
    		}
    		if($product->data($_POST)->add()){
    			echo "<script>alert('添加成功');location.href='".__URL__."/product_manage';</script>";
    		}else{
    			echo "<script>alert('添加失败');history.back();</script>";
    		}

    	}else{
    		$product=M('Product');
    		$bigcategory=M('Bigcategory');
    		$cate=$bigcategory->select();

    		$this->assign('cate',$cate);
    		$plate=M('Plate');
    		$plate=$plate->select();
    		$this->assign('plate',$plate);

	    	$allpro=$product->field('think_product.*,b.big_category_name')->join('left join think_bigcategory b on b.id=think_product.pid')->order('b.id')->select();

	    	$this->assign('allpro',$allpro);

	    	$art_classify = M("ArticleClassify");
    		$ac_data=$art_classify->field('think_article_classify.*,b.big_category_name')->join('left join think_bigcategory b on b.id=think_article_classify.pid')->select();
    		$this->assign('ac_data',$ac_data);
    		$this->display();
    	}
    }

	//产品下载购买信息
    public function product_download_buy_info(){

    	$bigcategory=M('Bigcategory');
    	$cate=$bigcategory->select();
    	$this->assign('cate',$cate);
//    	$plate=M('Plate');
//    	$plate=$plate->select();
//    	$this->assign('plate',$plate);
    	$dy = M('DownloadBuy');
    	$where = ' think_download_buy.dlid = 0 ';
    	if(!empty($_POST['t_time']) && !empty($_POST['e_time'])){
    		$where .= " and think_download_buy.time >= '".$_POST['t_time']."' and think_download_buy.time <= '".$_POST['e_time']."' ";
    	}else{
    		$where .= " and think_download_buy.time = '".date('Y-m-d')."' ";
    	}
    	$this->assign('s_time',$_POST['t_time']);
    	$this->assign('e_time',$_POST['e_time']);
    	if(!empty($_POST['plate'])){
    		$cid = $_POST['plate'];
    		$where .= " and b.id = $cid";
    		$this->assign('b_id',$cid);

    	}
    	$pdata = $dy->field('think_download_buy.*,sum(think_download_buy.down_num) as d_num,ROUND(sum(think_download_buy.buy_num)/count(think_download_buy.pid),0) as b_num,pro.product_name,b.big_category_name')
    	->join('left join think_product pro on pro.id=think_download_buy.pid left join think_bigcategory b on b.id=pro.pid')
    	->group('think_download_buy.pid')->where($where)->select();

    	$this->assign('product',$pdata);
    	$this->display();

    }

	public function down_buy_link()
    {

    	$product=M('Product');
    	$bigcategory=M('Bigcategory');
    	$plate=M('Plate');
    	$pid=$_GET['pid'];

    	$this->assign('host','http://'.$_SERVER['HTTP_HOST']);
    	if($_GET['link']==2){
    		$pdata=$product->field('think_product.*,pdl.id as pdlid, pdl.name as pdlname,pdl.url as pdlurl')->join('think_product_download_link pdl on pdl.pid=think_product.id')->where('think_product.one_download_link=0')->select();
    	}else{
	    	if(isset($pid) && $pid != ''){
	    		$pdata=$product->where('pid='.$pid.' and one_download_link=1')->select();
	    	}else{
	    		$pdata=$product->where('one_download_link=1')->select();
	    	}
    	}
    	foreach ($pdata as $k => $v){
	    	$pdata[$k]['cate_data']=$bigcategory->where('id='.$v['pid'])->find();
//    		$pdata[$k]['plate_data']=$plate->where('id='.$v['pid'])->find();
    	}
    	$this->assign('link',$_GET['link']);
    	$this->assign('product',$pdata);
    	$this->display();
    }


	//产品多链接管理
    public function product_download_link_manage(){

    	$pdl=M('ProductDownloadLink');
    	$cate=$pdl->select();
    	$this->assign('cate',$cate);
    	$this->display();
    }



	//添加产品多个链接
	public function product_download_link(){

		if($this->isPost()){
			$pdl=M('ProductDownloadLink');
			$insert['name']=isset($_POST['name'])?trim($_POST['name']):'';
			$insert['url']=isset($_POST['url'])?trim($_POST['url']):'';
			$insert['pid']=isset($_POST['pid'])?intval($_POST['pid']):0;
    		if(!$insert['url'] || !$insert['pid']){
    			return;
    		}
			if($pdl->where("pid=".$insert['pid']." and name='".$insert['name']."'")->find()){
    			echo "<script>alert('下载链接名已存在，请填写其他名字');history.back();</script>";
    			return;
    		}
    		if($pdl->add($insert)){
    			echo "<script>alert('添加成功');location.href='".__URL__."/product_download_link_manage';</script>";
    		}else{
    			echo "<script>alert('添加失败');history.back();</script>";
    		}

    	}else{
    		$p=M('Product');
    		$cate=$p->where("one_download_link=0")->select();
    		$this->assign('pdata',$cate);
    		$this->display();
    	}
    }

	     //修改产品信息
     public function modify_product(){

    	if($this->isPost()){
			$product=M('Product');
			$_POST['relative_product']=isset($_POST['rp'])?trim($_POST['rp'],','):'';
			if (get_magic_quotes_gpc()) {
				$_POST['content'] = isset($_POST['product_content'])?stripslashes($_POST['product_content']):'';
				$_POST['sec_description'] = isset($_POST['sec_description'])?stripslashes($_POST['sec_description']):'';
			}else{
				$_POST['content'] = $_POST['product_content'];
			}
    		if(!$_POST['product_id'] || !$_POST['product_name']){
    			return;
    		}
			if($product->where("id!=".$_POST['product_id']."  and pid=".$_POST['pid']." and product_name='".$_POST['product_name']."'")->find()){
    			echo "<script>alert('产品名已存在，请填写其他产品名');history.back();</script>";
    			return;
    		}
    		if(!$_FILES){
    			return;
    		}
    		$width=isset($_POST['width'])?trim($_POST['width']):0;
    		$height=isset($_POST['height'])?trim($_POST['height']):0;
    		$updata=$this->up($width,$height);

    		if($updata){
    			foreach($updata as $v){
    				if($v['key'] == 't_img'){
    					$_POST['product_img']=$v['savename'];
    				}elseif($v['key'] == 'sec_img'){
    					$_POST['sec_img']=$v['savename'];
    				}elseif($v['key'] == 'three_img'){
    					$_POST['three_img']=$v['savename'];
    				}elseif($v['key'] == 'home_img'){
    					$_POST['home_img']=$v['savename'];
    				}
    			}
    		}
    		if($product->where("id=".$_POST['product_id'])->save($_POST)){
    			echo "<script>alert('修改成功');location.href='".__URL__."/product_manage';</script>";
    		}else{
    			echo "<script>alert('修改失败');history.back();</script>";
    		}

    	}else{
    		$id=$_GET['id'];
    		if(!$id){
    			return;
    		}
    		$product=M('Product');
	    	$bigcategory=M('Bigcategory');
	    	$plate=M('Plate');
	    	$pdata=$product->where('id='.$id)->find();

	    	$rpid = array();
	    	if(!empty($pdata['relative_product'])){
	    		$rpid = explode(',',$pdata['relative_product']);
	    	}
	    	$this->assign('rpid',$rpid);
	    	$allpro=$product->field('think_product.*,b.big_category_name')->join('left join think_bigcategory b on b.id=think_product.pid')->order('b.id')->select();
	    	foreach($allpro as $k => $v){
	    		if(in_array($v['id'],$rpid)){
	    			$allpro[$k]['select'] = '1';
	    		}
	    	}
	    	$this->assign('allpro',$allpro);

		    $pdata['cate_data']=$bigcategory->field('id,big_category_name')->select();
//	    	$pdata['plate_data']=$plate->where('id='.$pdata['pid'])->find();
    		$this->assign('product',$pdata);
    		$art_classify = M("ArticleClassify");
    		$ac_data=$art_classify->field('think_article_classify.*,b.big_category_name')->join('left join think_bigcategory b on b.id=think_article_classify.pid')->select();
    		$this->assign('ac_data',$ac_data);
    		$this->display();
    	}
     }




	//静态化产品页
 	public function product_statice(){
		header("Content-Type:text/html; charset=utf-8");
    	$id=$_GET['id'];
    	if(!$id){
    		return;
    	}
    	$product=M('Product');
    	$data=$product->find($id);
    	if(!$data['statice_name']){
//    		echo "<script>alert('请先为产品添加静态化名称');location.href='".__URL__."/modify_product/id/".$id."';</script>";
			echo $this->pro_no_statice_name;
    		return;
    	}
		$p=file_get_contents($this->root_name.'/Index/product_description/id/'.$id);
	   	if(file_put_contents($data['statice_name'].'.html', $p)){
	   		$product->where('id='.$id)->save(array('is_statice'=>'1'));
//	   		echo "<script>alert('静态化成功');location.href='".__URL__."/product_manage';</script>";
			echo $this->statice_success;

	   	}else{
//    			echo "<script>alert('静态化失败');history.back();</script>";
			echo $this->statice_fail;
    	}
    }






	//删除产品
    public function delete_product(){
    	header("Content-Type:text/html; charset=utf-8");

     	$id=$_GET['id'];
      	if (!$id){
     		return;
     	}
     	$product=M('Product');
     	$pdata=$product->find($id);
    	//删除大类同时删除相关静态化文件
     	if($pdata['statice_name'] && file_exists($pdata['statice_name'].'.html')){
     		unlink($pdata['statice_name'].'.html');
     	}
     	$ret=$product->where("id=$id")->delete();
    	if(!$ret){
     		echo $this->delete_fail;
     	}else{
     		echo $this->delete_success;
     	}
     }


	//改变产品排序
	 function changeSort(){
     	$val = $_POST['val'];
     	$id = $_POST['id'];
     	$type = $_POST['type'];
     	if($type == 'all'){
     		$sort['sortall'] = $val;
     	}elseif($type == 'p'){
     		$sort['psort'] = $val;
     	}
     	$product=M('Product');
     	if($product->where("id=".$id)->save($sort)){
     		echo 1;
     	}else{
     		echo 0;
     	}
     }


	//更改产品多链接
     public function modify_product_download_link(){

     	if($this->isPost()){
     		$id=isset($_POST['id'])?intval($_POST['id']):0;
     		$data['pid']=isset($_POST['pid'])?intval($_POST['pid']):0;
     		$data['name']=isset($_POST['name'])?trim($_POST['name']):'';
     		$data['url']=isset($_POST['url'])?trim($_POST['url']):'';

     		if(!$data['pid'] || !$data['url']){
     			return;
     		}
     		$pdl=M('ProductDownloadLink');
//     		if($pdl->where("pid=".$data['pid']." and name='".$data['name']."'")->find()){
//    			echo "<script>alert('下载链接名已存在，请填写其他名字');history.back();</script>";
//    			return;
//    		}
     		if($pdl->where("id=$id")->save($data)){
     			echo "<script>alert('修改成功');location.href='".__URL__."/product_download_link_manage';</script>";
     		}else{
     			echo "<script>alert('修改失败');history.back();</script>";
     		}
     	}else{
	     	$id=$_GET['id'];
	     	if(!$id){
	     		return;
	     	}
	     	$pdl=M('ProductDownloadLink');
	     	$p=M('Product');
	     	$data=$pdl->find($id);
	     	$this->assign('data',$data);

    		$cate=$p->where("one_download_link=0")->select();
    		$this->assign('pdata',$cate);
	     	$this->display();
     	}
     }



//删除产品多链接
    public function delete_product_download_link(){
    	header("Content-Type:text/html; charset=utf-8");

     	$id=$_GET['id'];
      	if (!$id){
     		return;
     	}
     	$pdl=M('ProductDownloadLink');
     	$ret=$pdl->where("id=$id")->delete();
    	if(!$ret){
			echo $this->delete_fail;
			return;
     	}else{
			echo $this->delete_success;
     	}
     }


	//产品多个下载购买信息
    public function dlproduct_download_buy_info(){

    	$bigcategory=M('Bigcategory');
    	$cate=$bigcategory->select();
    	$this->assign('cate',$cate);
    	$dy = M('DownloadBuy');
    	$where = ' think_download_buy.dlid != 0 ';
    	if(!empty($_POST['t_time']) && !empty($_POST['e_time'])){
    		$where .= " and think_download_buy.time >= '".$_POST['t_time']."' and think_download_buy.time <= '".$_POST['e_time']."' ";
    	}else{
    		$where .= " and think_download_buy.time = '".date('Y-m-d')."' ";
    	}
    	$this->assign('s_time',$_POST['t_time']);
    	$this->assign('e_time',$_POST['e_time']);
    	if(!empty($_POST['plate'])){
    		$cid = $_POST['plate'];
    		$where .= " and b.id = $cid";
    		$this->assign('b_id',$cid);

    	}
    	$pdata = $dy->field('think_download_buy.*,sum(think_download_buy.dldown_num) as d_num,pro.product_name,b.big_category_name,pdl.name as pdlname')
    	->join('left join think_product pro on pro.id=think_download_buy.pid left join think_bigcategory b on b.id=pro.pid left join think_product_download_link pdl on pdl.id=think_download_buy.dlid')
    	->group('think_download_buy.dlid')->where($where)->select();

    	$this->assign('product',$pdata);
    	$this->display();

    }









	 //上传文件
    private function up($width=0,$height=0){
    	$width=$width==0?'150':$width;
    	$height=$height==0?'100':$height;
    	import('@.Org.UploadFile');
			$upload=new UploadFile();
			$upload->maxSize='1000000';  //是指上传文件的大小，默认为-1,不限制上传文件大小bytes
			$upload->savePath='./Public/upload/';       //上传保存到什么地方？路径建议大家已主文件平级目录或者平级目录的子目录来保存
			$upload->saveRule=uniqid;    //上传文件的文件名保存规则  time uniqid  com_create_guid  uniqid
			//$upload->autoCheck=false   ;  //是否自动检测附件
			$upload->uploadReplace=true;     //如果存在同名文件是否进行覆盖
			$upload->allowExts=array('jpg','jpeg','png','gif');     //准许上传的文件后缀
			$upload->allowTypes=array('image/png','image/jpg','image/pjpeg','image/gif','image/jpeg');  //检测mime类型
			$upload->thumb=true;   //是否开启图片文件缩略
			$upload->thumbMaxWidth=$width;  //以字串格式来传，如果你希望有多个，那就在此处，用,分格，写上多个最大宽
			$upload->thumbMaxHeight=$height;	//最大高度
			$upload->thumbPrefix='s_,m_';//缩略图文件前缀
			//$upload->thumbSuffix='_s,_m';  //文件后缀
			//$upload->thumbPath='' ;  // 如果留空直接上传至
			//$upload->thumbFile   在数据库当中也存一个文件名即可
			$upload->thumbRemoveOrigin=0;  //如果生成缩略图，是否删除原图 1是0否
			//$upload->autoSub   是否使用子目录进行保存上传文件
			//$upload->subType=''   子目录创创方式默认为hash 也可以设为date
			//$upload->dateFormat  子目录方式date的指定日期格式
			//$upload->hashLevle

		if(!$upload->upload()) { //  上传错误 提示错误信息

			//$this->error($upload->getErrorMsg());
			return false;

		}else{ //  上传成功 获取上传文件信息

			return $upload->getUploadFileInfo();

		 }
    }


	//添加栏目
	public function plate(){

		if($this->isPost()){
			$plate=M('Plate');
			$insert['plate_name']=isset($_POST['plate_name'])?trim($_POST['plate_name']):'';
			$insert['plate_title']=isset($_POST['plate_title'])?trim($_POST['plate_title']):'';
			$insert['pid']=isset($_POST['pid'])?trim($_POST['pid']):0;
			$insert['plate_keyword']=isset($_POST['plate_keyword'])?trim($_POST['plate_keyword']):0;
			$insert['statice_name']=isset($_POST['statice_name'])?trim($_POST['statice_name']):'';
			$insert['win_mac_classfy']=isset($_POST['win_mac_classfy'])?trim($_POST['win_mac_classfy']):'1';
			$insert['plate_description']=isset($_POST['plate_dis'])?trim($_POST['plate_dis']):'';
    		$insert['add_time']=time();
    		if(!$insert['plate_name'] || !$insert['pid']){
    			return;
    		}
			if($plate->where("pid=".$insert['pid']." and plate_name='".$insert['plate_name']."'")->find()){
    			echo "<script>alert('栏目名已存在，请填写其他栏目名');history.back();</script>";
    			return;
    		}
    		/*$ret=true;
    		$explode=explode(';', $data['plate_name']);
    		foreach($explode as $k => $v){
    			$insert['plate_name']=$v;
	    		if(!$plate->add($insert)){
	    			$ret=false;
	    		}
    		}*/
    		if($plate->add($insert)){
    			echo "<script>alert('添加成功');location.href='".__URL__."/plate_manage';</script>";
    		}else{
    			echo "<script>alert('添加失败');history.back();</script>";
    		}

    	}else{
    		$bigcategory=M('Bigcategory');
    		$cate=$bigcategory->select();
    		$this->assign('cate',$cate);
    		$this->display();
    	}
    }

	//更改栏目名称
     public function modify_plate(){

     	if($this->isPost()){
     		$pid=isset($_POST['pid'])?intval($_POST['pid']):0;
     		$id=isset($_POST['plate_id'])?intval($_POST['plate_id']):0;
     		$plate_name=isset($_POST['change_name'])?trim($_POST['change_name']):'';
     		$plate_title=isset($_POST['plate_title'])?trim($_POST['plate_title']):'';
     		$plate_keyword=isset($_POST['plate_keyword'])?trim($_POST['plate_keyword']):'';

     		if(!$id || !$plate_name){
     			return;
     		}
     		$plate=M('Plate');
     		$data['plate_name']=$plate_name;
     		$data['plate_title']=$plate_title;
     		$data['win_mac_classfy']=isset($_POST['win_mac_classfy'])?trim($_POST['win_mac_classfy']):'1';
     		if($plate->where("id!=".$id." and pid=".$pid." and plate_name='".$plate_name."'")->find()){
    			echo "<script>alert('此分类栏目名已存在，请填写其他栏目名');history.back();</script>";
    			return;
    		}
     		if($plate_keyword){
     			$data['plate_keyword']=$plate_keyword;
     		}
     		if(isset($_POST['plate_dis']) && $_POST['plate_dis']!=''){
     			$data['plate_description']=isset($_POST['plate_dis'])?trim($_POST['plate_dis']):'';
     		}
     		$data['statice_name']=isset($_POST['statice_name'])?trim($_POST['statice_name']):'';
     		if($plate->where("id=$id")->save($data)){
     			echo "<script>alert('修改成功');location.href='".__URL__."/plate_manage';</script>";
     		}else{
     			echo "<script>alert('修改失败');history.back();</script>";
     		}
     	}else{
	     	$plate=M('Plate');
	     	$id=$_GET['id'];
	     	if(!$id){
	     		return;
	     	}
	     	$data=$plate->find($id);
	     	$this->assign('plate',$data);
	     	$this->display();
     	}
     }




	//删除栏目---加的
    public function delete_plate(){
    	header("Content-Type:text/html; charset=utf-8");

     	$id=$_GET['id'];
      	if (!$id){
     		return;
     	}
     	$plate=M('Plate');
     	$article=M('Article');
     	$product=M('Product');

     	$pdata=$plate->find($id);
     	$prodata=$product->where('pid='.$id)->select();
     	$adata=$article->where('pid='.$id)->select();

     	//删除大类同时删除相关静态化文件
     	if($pdata['statice_name'] && file_exists($pdata['statice_name'].'.html')){
     		unlink($pdata['statice_name'].'.html');
     	}
     	$this->delete_statice_file($prodata);
     	$this->delete_statice_file($adata);

     	$pret=$plate->where("id=$id")->delete();
     	$aret=$article->where("pid=$id")->delete();
     	$prret=$product->where("pid=$id")->delete();
    	if(!$pret){
//     		echo "<script>alert('删除栏目失败');history.back();</script>";
     		echo $this->delete_fail;
     		return;
     	}else{
     		echo $this->delete_success;
     		return;
//     		echo "<script>alert('删除栏目成功');location.href='".__URL__."/plate_manage';</script>";
     	}
     }

	//静态化栏目页
 	public function plate_statice(){
		header("Content-Type:text/html; charset=utf-8");
    	$id=$_GET['id'];
    	$pid=$_GET['pid'];
    	if(!$id || !$pid){
    		return;
    	}
    	$plate=M('Plate');
    	$data=$plate->find($id);
    	if(!$data['statice_name']){
//    		echo "<script>alert('请先为栏目添加静态化名称');location.href='".__URL__."/modify_plate/pid/".$pid."/id/".$id."';</script>";
    		echo $this->plate_no_statice_name;
    		return;
    	}
		$p=file_get_contents($this->root_name.'/Index/classify/id/'.$id);
	   	if(file_put_contents($data['statice_name'].'.html', $p)){
	   		$plate->where('id='.$id)->save(array('is_statice'=>'1'));
//	   		echo "<script>alert('静态化成功');location.href='".__URL__."/plate_manage';</script>";
			echo $this->statice_success;
			return;
	   	}else{
//    			echo "<script>alert('静态化失败');history.back();</script>";
			echo $this->statice_fail;
			return;
    	}
    }

    public function changeStatus(){
    	header("Content-Type:text/html; charset=utf-8");
    	$id=$_POST['id'];
    	if (!$id){
    		return;
    	}
    	$product=M('Product');
    	$pdata=$product->find($id);
    	$status = $pdata['status']==1?0:1;
    	//删除大类同时删除相关静态化文件
    	//     	if($pdata['statice_name'] && file_exists($pdata['statice_name'].'.html')){
    	//     		unlink($pdata['statice_name'].'.html');
    	//     	}
    	$ret=$product->where("id=$id")->save(array('status'=>$status));
    	if(!$ret){
    		echo 2;
    	}else{
    		echo $status;
    	}
    }
    

}